import json
import pymysql

# 连接到MySQL数据库
conn = pymysql.connect(
    host='112.124.12.135',
    user='root',
    password='root',
    database='pressure'
)

# 创建数据库游标
cur = conn.cursor()

# 执行查询语句
query = """
    SELECT JSON_ARRAYAGG(
      JSON_OBJECT(
        'date', date,
        'high_pressure', high_pressure,
        'low_pressure', low_pressure
      )
    ) AS data
    FROM blood_pressure
"""
cur.execute(query)

# 获取查询结果
result = cur.fetchone()[0]

# 将查询结果转换成JSON格式
data = json.loads(result)

# 将JSON数据保存到文件中
file_path = 'data.json'
with open(file_path, 'w') as f:
    json.dump(data, f, indent=2)

# 关闭数据库连接
cur.close()
conn.close()
